package com.wangtao.firstapp.fragments.homefragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.HomeVPAdapter;
import com.wangtao.firstapp.adapters.MessageAdapter;
import com.wangtao.firstapp.fragments.messagefragments.CommentFragment;
import com.wangtao.firstapp.fragments.messagefragments.NoticeFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息界面
 * Created by lanouhn on 16/8/24.
 */
public class MessageFragment extends Fragment {
    private ArrayList<String> tabnames;
    private ArrayList<Fragment> fragments;
    private MessageAdapter messageAdapter;
    private ViewPager viewpager_message;
    private TabLayout tablayout_message_top;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.messagefragment, null);
        viewpager_message = (ViewPager) view.findViewById(R.id.viewpager_message);
        tablayout_message_top = (TabLayout) view.findViewById(R.id.tablayout_message_top);
        initData();
        initEvent();
        return view;
    }

    private void initData() {
        tabnames = new ArrayList<>();
        tabnames.add("通知");
        tabnames.add("评论");
        fragments = new ArrayList<>();
        NoticeFragment noticeFragment = new NoticeFragment();
        CommentFragment commentFragment = new CommentFragment();
        fragments.add(noticeFragment);
        fragments.add(commentFragment);
        messageAdapter = new MessageAdapter(getFragmentManager(), tabnames, fragments);
        viewpager_message.setAdapter(messageAdapter);
        tablayout_message_top.setupWithViewPager(viewpager_message);
    }

    private void initEvent() {
        tablayout_message_top.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        viewpager_message.setCurrentItem(0);
                        break;
                    case 1:
                        viewpager_message.setCurrentItem(1);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
